package xxx;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentMethod {//实现学生功能的类
    Scanner sc = new Scanner(System.in);
    public static ArrayList<Student> stu = new ArrayList<>();


    public void start(){//操作台
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("JavaLs06\\studnet.txt"));
        ){
            ArrayList<Student> arrayList = (ArrayList) ois.readObject();
            stu.addAll(arrayList);
        } catch (Exception e) {
        }
        while (true) {
            System.out.println("-----------欢迎来到学生管理系统-----------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查看学生");
            System.out.println("5.QQ通讯");
            System.out.println("6.退出");
            System.out.println("请输入您的选择:");
            int temp = sc.nextInt();
            switch (temp){
                case 1:
                    addStudent();
                    break;
                case 2:
                    delet();
                    break;
                case 3:
                    revise();
                    break;
                case 4:
                    Inquire();
                    break;
                case 5:
                    communication();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("您输入的指令不正确请重新确认!~~");
                    break;
            }
        }

    }

    private void communication() {//qq通讯
        while (true) {
            System.out.println("请输入好友的QQ号");
            String QQid = sc.next();
            for (int i = 0; i < stu.size(); i++) {
                Student student = stu.get(i);
                if (QQid.equals(student.getQQid())){
                    try {
                        Socket socket = new Socket("127.0.0.1",9999);
                        new SocketThread(socket).start();
                        OutputStream os = socket.getOutputStream();
                        DataOutputStream dos = new DataOutputStream(os);
                        System.out.println("成功建立连接!~~");
                        while (true){
                            Scanner sca = new Scanner(System.in);
                            String rs = sca.nextLine();
                            if ("exit".equals(rs)){
                                System.out.println("欢迎您下次再来!~~");
                                dos.close();
                                socket.close();
                                return;
                            }
                            dos.writeUTF(student.getName()+" :"+rs);
                            dos.flush();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else {
                    System.out.println("请重新输入");
                }
            }
        }

    }

    private void Inquire() {//查询功能
        if (stu.size()<1){
            System.out.println("现在还没有任何学生!~~请先去添加学生!");
            return;
        }
        try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream("JavaLs06\\studnet.txt"));
        )
        {
            ArrayList<Student> arraylist = (ArrayList) ois.readObject();
            for (int i = 0; i < arraylist.size(); i++) {
                Student student1 = arraylist.get(i);
                System.out.print(student1.getId()+"  ");
                System.out.print(student1.getName()+"  ");
                System.out.print(student1.getAge()+"  ");
                System.out.print(student1.getHomeaddress()+"  ");
                System.out.println(student1.getQQid()+"  ");
            }
        } catch (Exception e) {
            System.out.println("现在还没有任何学生!~~请先去添加学生!");
        }
    }

    private void revise() {//修改功能
        if (stu.size()<1){
            System.out.println("现在还没有任何学生!~~请先去添加学生!");
            return;
        }
        try (
                ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("JavaLs06\\studnet.txt"));
                ){
            System.out.println("请输入要修改的学生id!~~");
            String id = sc.next();
            if(judgementid(id)){
                for (int i = 0; i < stu.size(); i++) {
                    Student student1 = stu.get(i);
                    if (student1.getId().equals(id)) {
                        System.out.println("请输入您的姓名!");
                        String name = sc.next();
                        student1.setName(name);
                        System.out.println("请输入您的年龄");
                        int age = sc.nextInt();
                        student1.setAge(age);
                        System.out.println("请输入家庭住址!~~");
                        String homeaddress = sc.next();
                        student1.setHomeaddress(homeaddress);
                        System.out.println("修改成功!~~");
                        obj.writeObject(stu);
                        return;
                    }
                }
            }else {
                System.out.println("您输入的id不存在!~~");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    private void delet() {
        if (stu.size()<1){
            System.out.println("现在还没有任何学生!~~请先去添加学生!");
            return;
        }
        try (
                ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream("JavaLs06\\studnet.txt"));
                ){
            System.out.println("请输入您要删除的学生id");
            String id = sc.next();
            if (judgementid(id)) {
                for (int i = 0; i < stu.size(); i++) {
                    Student student1 = stu.get(i);
                    if (student1.getId().equals(id)) {
                        stu.remove(student1);
                        obj.writeObject(stu);
                        return;
                    }
                }
                System.out.println("删除成功!~~");
                return;
            }else {
                System.out.println("id不存在");
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private void addStudent() {//添加学生功能
        Student student1 = new Student();
        while (true) {
            System.out.println("请输入学生id");
            String id = sc.next();
            if (!judgementid(id)){
                student1.setId(id);
                break;
            }else {
                System.out.println("您输入的id重复");
            }
        }
        System.out.println("请输入您的姓名!");
        String name = sc.next();
        student1.setName(name);
        System.out.println("请输入您的年龄");
        int age = sc.nextInt();
        student1.setAge(age);
        System.out.println("请输入家庭住址!~~");
        String homeaddress = sc.next();
        student1.setHomeaddress(homeaddress);
        System.out.println("请输入QQid~~");
        String QQid = sc.next();
        student1.setQQid(QQid);
        System.out.println("添加成功!~~");
        stu.add(student1);
        try (
                ObjectOutputStream ois = new ObjectOutputStream(new FileOutputStream("JavaLs06\\studnet.txt"));

        ){
            ois.writeObject(stu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private boolean judgementid(String id){//判断id是否唯一的功能

            for (int i = 0; i <stu.size() ; i++) {
                Student student = stu.get(i);
                if (student.getId().equals(id)) {
                    return true;
                }
            }
            return false;//没有找到

    }
}
